from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QTableWidgetItem, QApplication
from PyQt5.QtCore import QTranslator
from classes.engine import Engine
from ui_py.page2_ui import Main_Ui
from temp import Excel, ReadExcel
from validation import validation
import json
from error import Error
from expert import Expert
from dataExpert import Data
class Page2(QtWidgets.QWidget, Main_Ui):
    engine = Engine()
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.setupUi(self)
        with open('lang.json', encoding='utf-8') as file:
            data = json.load(file)
        if data['lang'] != "":
            self.trans = QTranslator(self)
            self.trans.load('translate/' + 'page2')
            _app = QApplication.instance() # получить экземпляр приложения
            _app.installTranslator(self.trans)
        self.n = 5
        self.err = Error()
        self.data = Data()
        self.expert = Expert()
        self.err.clear = 0
        if self.engine.cil == "L6" or self.engine.cil == "V12":
            self.n = 8
        elif self.engine.cil == "L4" or self.engine.cil == "V8":
            self.n = 6
        self.tableWidget.blockSignals(True)
        row = self.tableWidget.rowCount()
        self.tableWidget.insertRow(row)
        if data['lang'] != "":
            page = data['en']['page2']
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][0]))
        else:
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Диаметр шатунной шейки; dsch"))
        try:
            value = str(self.data.excel['dsch'])
        except:
            value = ""
        self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
        if data['lang'] != "":
            self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
        else:
            self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
        row = self.tableWidget.rowCount()
        self.tableWidget.insertRow(row)
        if data['lang'] != "":
            page = data['en']['page2']
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][1]))
        else:
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Диаметр коренной шейки; dkor"))
        try:
            value = str(self.data.excel['dkor'])
        except:
            value = ""
        self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
        if data['lang'] != "":
            self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
        else:
            self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
        row = self.tableWidget.rowCount()
        self.tableWidget.insertRow(row)
        if data['lang'] != "":
            page = data['en']['page2']
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][2]))
        else:
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Ширина шатунного подшипника; bsch"))
        try:
            value = str(self.data.excel['bsch'])
        except:
            value = ""
        self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
        if data['lang'] != "":
            self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
        else:
            self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
        for i in range(1, self.n):
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][3].format(i, i)))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Ширина {0}-го коренного подшипника; bkor{1}".format(i, i)))
            try:
                value = str(self.data.excel['bkor{0}'.format(i)])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
        for i in range(1, self.n):
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][4].format(i, i)))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Ширина канавки в {0}-м коренном подшипнике; bkan{1}".format(i, i)))
            try:
                value = str(self.data.excel['bkan{0}'.format(i)])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
        for i in range(1, self.n-1):
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][5].format(i, i+1)))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние между {0} и {1} коренными подшипниками; dl{0}{1}".format(i, i+1)))
            try:
                value = str(self.data.excel['dl{0}{1}'.format(i, i+1)])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
        for i in range(1, self.n-1):
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][6].format(i, i, i)))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние от {0} коренного подшипника до {1} цилиндра; dz{2}".format(i, i, i)))
            try:
                value = str(self.data.excel['dz{0}'.format(i)])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
        if self.engine.cil == "V6":
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][7]))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние от 1 коренного подшипника до 4 цилиндра; dz4"))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
            try:
                value = str(self.data.excel['dz4'])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
        if self.engine.cil == "V8":
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][8]))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние от 1 коренного подшипника до 5 цилиндра; dz5"))
            try:
                value = str(self.data.excel['dz5'])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))

        if self.engine.cil == "V12":
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][9]))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние от 1 коренного подшипника до 7 цилиндра; dz7"))
            try:
                value = str(self.data.excel['dz7'])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))

            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][10]))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние от 2 коренного подшипника до 8 цилиндра; dz8"))
            try:
                value = str(self.data.excel['dz8'])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][11]))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние от 3 коренного подшипника до 9 цилиндра; dz9"))
            try:
                value = str(self.data.excel['dz9'])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][12]))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние от 4 коренного подшипника до 10 цилиндра; dz10"))
            try:
                value = str(self.data.excel['dz10'])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][13]))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние от 5 коренного подшипника до 11 цилиндра; dz11"))
            try:
                value = str(self.data.excel['dz11'])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][14]))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние от 6 коренного подшипника до 12 цилиндра; dz12"))
            try:
                value = str(self.data.excel['dz12'])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
        if self.engine.cil == "V6":
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][15]))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние от 2 коренного подшипника до 5 цилиндра; dz5"))
            try:
                value = str(self.data.excel['dz5'])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][16]))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние от 3 коренного подшипника до 6 цилиндра; dz6"))
            try:
                value = str(self.data.excel['dz6'])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
        if self.engine.cil == "V8":
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][17]))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние от 2 коренного подшипника до 6 цилиндра; dz6"))
            try:
                value = str(self.data.excel['dz6'])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][18]))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние от 3 коренного подшипника до 7 цилиндра; dz7"))
            try:
                value = str(self.data.excel['dz7'])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            if data['lang'] != "":
                page = data['en']['page2']
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][19]))
            else:
                self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Расстояние от 4 коренного подшипника до 8 цилиндра; dz8"))
            try:
                value = str(self.data.excel['dz8'])
            except:
                value = ""
            self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
            if data['lang'] != "":
                self.tableWidget.setItem(row, 1, QTableWidgetItem("mm"))
            else:
                self.tableWidget.setItem(row, 1, QTableWidgetItem("мм"))
        self.tableWidget.blockSignals(False)
        self.tableWidget.cellChanged.connect(self.cellChange)
    def cellChange(self, row, column):
        key = self.tableWidget.verticalHeaderItem(row).text().split('; ')[1]
        if validation(self.tableWidget.item(row, column).text(), key, self.data.excel):
            self.tableWidget.blockSignals(True)
            self.tableWidget.item(row, column).setBackground(QtGui.QColor(112, 255, 127))
            self.tableWidget.blockSignals(False)
            self.data.excel[key] = self.tableWidget.item(row, column).text()
            # excel = ReadExcel(self.filename)
            # excel.updateCell(key, self.tableWidget.item(row, column).text(), self.filename)
            self.expert.excel[key] = self.tableWidget.item(row, column).text()
            try:
                if self.err.error_dict[key] > 0:
                    del self.err.error_dict[key]
            except:
                pass
        else:
            self.tableWidget.blockSignals(True)
            self.tableWidget.item(row, column).setBackground(QtGui.QColor(255, 152, 152))
            self.tableWidget.blockSignals(False)
            self.err.error_dict[key] = 2
            self.err.page = 2
    # def press(self, row, column):
    #     self.tableWidget.cellChanged.connect(self.cellChange)